//
//  TravelNoteViewController.m
//  B
//
//  Created by Sunpeng on 15/11/9.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "TravelNoteViewController.h"
#import "TravelNoteTableViewCell.h"
#import "HTTPTool.h"
#import "BaseView.h"
#import "ListDetailViewController.h"
@interface TravelNoteViewController () <UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *noteArray;

@end

@implementation TravelNoteViewController
//-(void)viewWillAppear:(BOOL)animated {
//    
//    
//    [super viewWillAppear:animated];
//    
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = YES;
//        }
//    }
//}
//
//- (void) viewWillDisappear:(BOOL)animated {
//    
//    [super viewWillDisappear:animated];
//    for (UIView *view in self.tabBarController.view.subviews) {
//        if ([view isKindOfClass:[BaseView class]]) {
//            view.hidden = NO;
//            self.hidesBottomBarWhenPushed = YES;
//        }
//    }
//    
//    
//}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //数组初始化
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        
        self.hidesBottomBarWhenPushed = YES;
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"精品游记";
    //返回键
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
//    
//    -(void)leftButtonAction
//    {
//        [self.navigationController popToRootViewControllerAnimated:NO];
//    }
    NSDictionary *dict=[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-64) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.noteArray = [NSMutableArray array];
    [self afnGetRequest];
}
-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
-(void)afnGetRequest
{
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/?start=0&count=20",self.typeNumber,self.idString] body:nil httpResult:JSON success:^(id result) {
        //http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld
        
        
        NSDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"items"];
        if (array.count == 0) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有相关信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertView show];
        }
        for (NSMutableDictionary *dic in array) {
            TravelNoteModel *noteM = [[TravelNoteModel alloc]init];
            [noteM setValuesForKeysWithDictionary:dic];
            noteM.idString = [dic objectForKey:@"id"];
            //NSLog(@"noteM.idNumber = %@",noteM.idString);
            [self.noteArray addObject:noteM];
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        NSLog(@"dddddd");
    }];



}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.noteArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelNoteModel *noteM = [self.noteArray objectAtIndex:indexPath.row];
    //NSLog(@"222%@",noteM.name);
    TravelNoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"noteCell"];
    if (cell == nil) {
        cell = [[TravelNoteTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"noteCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.noteM = noteM;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelNoteModel *noteM = [self.noteArray objectAtIndex:indexPath.row];
    ListDetailViewController *detailVC = [[ListDetailViewController alloc]init];
    detailVC.url = [NSString stringWithFormat:@"http://breadtrip.com/trips/%@/",noteM.idString];
    [self.navigationController pushViewController:detailVC animated:NO];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 0.24*HIGHT;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
